<?php
	/*
		@教学点播处理
	*/
	
	//处理方法
	if(empty($action) || !isset($action)){
		$action = "default";
	}else{
		$action = $_GET['action'];
	}
	//$action = (empty($action) || !isset($action)) ? "default" : $_GET['action'];

	
	
	if($action == "default"){
		//定义每页显示多少条
		$perShow = 10;
		
		include_once("configs/database.inc.php");		//引入数据库配置
		$D->query("set names utf8");
		
		//取得所有记录数
		$totalRows = $D->rowsAll('broadcast');
		if($totalRows < $perShow){
			//进行数据库查询
			$res_broadcast = $D->fetchAllQuery("SELECT * FROM `broadcast` ORDER BY `uploadtime` DESC LIMIT 0 , ".$perShow."");

			$html_page = "<span style=\"color:red;\">暂无分页！</span> ";
		}else{
			@$page = $_GET['page'];
			//处理页面
			if(empty($page) || !isset($page)){
				$page = 0;	
			}else{
				$page = $_GET['page'];	
			}
			
			//上一页，下一页处理
			if($page == 0){
				$pre_page = 0;
				$next_page = 1;
			}else{
				$pre_page = $page - 1;	
				$next_page = $page + 1;
			}
			
			//处理最后一页
			$final_page = floor($totalRows / $perShow);
			
			//处理下一页越界
			if($next_page > $final_page){
				$next_page = $final_page;	
			}
			
			//取得开始查询的行数
			$offset = $page * $perShow;
			
			//进行数据库查询
			$res_broadcast = $D->fetchAllQuery("SELECT * FROM `broadcast` ORDER BY `uploadtime` DESC LIMIT ".$offset." , ".$perShow."");
					
			//测试输出
			$html_page = "共<span style=\"color:red;\">".$totalRows."</span>条记录 <a href=\"index.php?controller=broadcast&action=default&page=0\">首页</a> | <a href=\"index.php?controller=broadcast&action=default&page=".$pre_page."\">上一页</a> | <a href=\"index.php?controller=broadcast&action=default&page=".$next_page."\">下一页</a> | <a href=\"index.php?controller=broadcast&action=default&page=".$final_page."\">最后一页</a>";
		}
		$smarty->assign('title','先锋视频--教学点播');
		$smarty->assign('res_broadcast',$res_broadcast);
		$smarty->assign('html_page',$html_page);
		$smarty->display('broadcast.tpl');
	}else if($action == "submit"){
		//取得数据参数
		$title = $_POST['title'];
		$userinfo = $_POST['userinfo'];
		$content = $_POST['content'];
		
		include_once("configs/database.inc.php");		//引入数据库配置
		
		//操作 
		if($D->connect()){
			$data = array(
						  	"title"		=> $title,
							"userinfo"	=> $userinfo,
							"content"	=> $content,
							"postdate"	=> date("Y-m-d H:i")
						  );
			$D->query("set names utf8");
			if($D->insert("feedback",$data)){
				echo "<script>alert(\"你的意见已经加入到数据库，谢谢！\");location.href=\"index.php?controller=feedback\";</script>";
			}else{
				echo "<script>alert(\"网络故障，稍后再试！\");location.href=\"index.php?controller=feedback\";</script>";
					
			}	
		}else{
			echo "<script>alert(\"数据库服务器忙，稍后再试！\");location.href=\"index.php?controller=feedback\";</script>";
		}
	}
?>